package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.common.mvc.view.R;
import com.theme.passport.dao.theme.WallpaperDao;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.service.theme.WallpaperService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("wallpaperService")
public class WallpaperServiceImpl extends ServiceImpl<WallpaperDao, WallpaperEntity> implements WallpaperService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String keyword = (String) params.get("key");
        String free = (String) params.get("free");
        String nts = (String) params.get("nts");
        String status = (String) params.get("status");
        String classifyId = (String) params.get("classifyId");
        IPage<WallpaperEntity> page = this.page(
                new Query<WallpaperEntity>().getPage(params),
                new QueryWrapper<WallpaperEntity>()
                        .eq(StringUtils.isNotBlank(nts),"nts", nts)
                        .eq(StringUtils.isNotBlank(free),"free", free)
                        .eq(StringUtils.isNotBlank(userId),"user_id", userId)
                        .eq(StringUtils.isNotBlank(classifyId), "classify_id", classifyId)
                        .in(StringUtils.isNotBlank(status), "status", status)
                        .like(StringUtils.isNotBlank(keyword), "name", keyword)
                        .orderByDesc("updated")
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByStatus(Map<String, Object> params) {
        String status = (String) params.get("status");
        List<String> statusList = new ArrayList<>();
        if (StringUtils.isNotEmpty(status)){
            statusList.add(status);
        }else {
            statusList.add("1");
            statusList.add("2");
        }
        String keyword = (String) params.get("key");
        String classifyId = (String) params.get("classifyId");
        IPage<WallpaperEntity> page = this.page(
                new Query<WallpaperEntity>().getPage(params),
                new QueryWrapper<WallpaperEntity>()
                        .in(statusList.size()>0,"status", statusList)
                        .eq(StringUtils.isNotBlank(classifyId), "classify_id", classifyId)
                        .like(StringUtils.isNotBlank(keyword), "name", keyword)
                        .orderByDesc("updated")
        );
        return new PageUtils(page);
    }

    @Override
    public R saveWallpaper(WallpaperEntity wallpaper) {
        wallpaper.setCreated(new Date());
        wallpaper.setUpdated(new Date());
        this.save(wallpaper);
        return R.ok();
    }

    @Override
    public R updateWallpaper(WallpaperEntity wallpaper) {
        wallpaper.setUpdated(new Date());
        this.updateById(wallpaper);
        return R.ok();
    }

    @Override
    public PageUtils queryWallpaperByIds(Map<String, Object> params, List<Long> wallpapersIds) {
        String status = (String) params.get("status");
        IPage<WallpaperEntity> page = this.page(
                new Query<WallpaperEntity>().getPage(params),
                new QueryWrapper<WallpaperEntity>()
                        .eq(StringUtils.isNotBlank(status), "status", status)
                        .in(wallpapersIds.size()>0,"id", wallpapersIds)
                        .orderByDesc("updated")
        );
        return new PageUtils(page);
    }

}